<?

function get_module($name){
	global $sql;
	
	$var=$sql->request_single("select * from ".CFG_MYSQL_PREFIX."module where nom='".$name."'");
	if($var!==false)  return new module($var);
	else return false;	
}

function load_module($name,$mod=false){
	global $sql;
	
	if(!is_object($mod)){
		$mod=get_module($name);
		if($mod===false) return false;
	}
	
	if($mod->actif){
	
		// librairies
		if(is_dir("modules/".$name."/library")){
			$handle=opendir("modules/".$name."/library");
			while(false!==($file=readdir($handle))){
				if(strpos($file,"lib")!==false) require_once("modules/".$name."/library/".$file);
			}
		}

		// classes
		if(is_dir("modules/".$name."/classes")){
			$handle=opendir("modules/".$name."/classes");
			while(false!==($file=readdir($handle))){
				if(strpos($file,"class")!==false) require_once("modules/".$name."/classes/".$file);
			}
		}
	
		if(file_exists("modules/".$name."/main.php")) include "modules/".$name."/main.php";
		// JS
		if(file_exists("themes/".CFG_THEME."/".$name."/script.js")) $GLOBALS["VUE"]["includes"][]="themes/".CFG_THEME."/".$name."/script.js";
		elseif(file_exists("modules/".$name."/script.js")) $GLOBALS["VUE"]["includes"][]="modules/".$name."/script.js";
		// CSS
		if(file_exists("themes/".CFG_THEME."/".$name."/style.css")) $GLOBALS["VUE"]["includes"][]="themes/".CFG_THEME."/".$name."/style.css";
		elseif(file_exists("modules/".$name."/style.css")) $GLOBALS["VUE"]["includes"][]="modules/".$name."/style.css";
		// VIEW
		if(file_exists("themes/".CFG_THEME."/".$name."/view.phtml")) include "themes/".CFG_THEME."/".$name."/view.phtml";
		elseif(file_exists("modules/".$name."/view.phtml")) include "modules/".$name."/view.phtml";
	}

}

function get_children($module){
	global $sql;
	
	$tmp=array();
	$res=$sql->request("select nom from ".CFG_MYSQL_PREFIX."module where parentModule REGEXP '([^[:alnum:]]|^)".$module."([^[:alnum:]]|$)'");
	foreach($res as $var){
		$tmp[]=$var->nom;
	}
	return $tmp;
}

function load_params($module){
	global $sql;
	
	$cfg_params=array();
	$res=$sql->request("select * from ".CFG_MYSQL_PREFIX."module_config where module='".$module."'");
	foreach($res as $var){
		$cfg_params[$var->nom]=$var->valeur;
	}
	
	return $cfg_params;
}

function get_param($module,$nom){
	global $sql;
	$var=$sql->request_single("select * from ".CFG_MYSQL_PREFIX."module_config where module='".$module."' and nom='".$nom."'");
	if($var!==false) return $var->nom;
	else return false;
}

function set_param($module,$nom,$value){
	global $sql;
	$sql->query("update ".CFG_MYSQL_PREFIX."module_config set valeur='".addslashes($value)."' where nom='".$nom."' and module='".$module."'");
	$GLOBALS["CONFIG"][$module][$nom]=$value;
}
